//
//  PlayViewController.m
//  片刻
//
//  Created by lanou3g on 15/6/9.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "PlayViewController.h"
#import "DetailsViewController.h"
#import "MaocHeader.h"
#import "RadioModel.h"
#import "PlayerManager.h"
#import "playerCollectionViewCell.h"
#import "PlayerTableViewCell.h"
#import "UIImageView+WebCache.h"
@interface PlayViewController ()
@property (nonatomic,retain) UIScrollView *mainScroll;//创建主scroll;
@property (nonatomic,retain) RadioModel *model;
@property (nonatomic,retain) PlayerManager *player; //播放

//*******第一界面
@property (nonatomic,retain) UITableView *tabelView;
//*******第二页面
@property (nonatomic,retain) UIImageView *imageView; //大图片
@property (nonatomic,retain) UILabel *nameLabel;//名字
@property (nonatomic,retain) UILabel *Label;//喜欢和评论的 laber
//*******导航条
@property (nonatomic,retain) UIButton *switchButton;//开始和暂停
@property (nonatomic,assign) BOOL isPlayer;
//*******第四页面
@property (nonatomic,retain) UIView *fourView; //大View
@property (nonatomic,retain) UICollectionViewFlowLayout*layout; //布局
@property (nonatomic,retain) UICollectionView *colletion; //集合试图
@property (nonatomic,retain) NSMutableArray *fourArr; //第四个页面的数组
@property (nonatomic,retain) UIView *lineView; //分界线
@property (nonatomic,retain) UIImageView *userNameImage2;//作者图片
@property (nonatomic,retain) UIImageView *upNameImage1;
@property (nonatomic,retain) UILabel *userNameLabel1; //作者名字
@property (nonatomic,retain) UILabel *userNameLabel2; //原文作者名字

@end

@implementation PlayViewController
-(void)dealloc
{
    self.mainScroll = nil;
    self.model = nil;
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    _isPlayer = YES;
    self.fourArr = [NSMutableArray array];//初始化
    [self createData];
    [self createScrollView];   // 添加 scrollView
    [self createTabelView]; //添加第一个试图
    [self createPlayerNavi];   // 添加底下导航控制器
    [self createWebView];
   
    

}

-(void)createScrollView{
    self.mainScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HiGHT - 120)]; //大小
    _mainScroll.contentSize = CGSizeMake(SCREEN_WIDTH * 4, SCREEN_HiGHT-120); //内容大小
    _mainScroll.contentOffset = CGPointMake(SCREEN_WIDTH , 0);
    _mainScroll.pagingEnabled = YES; //是否整平滑动
//    _mainScroll.bounces = NO; // 是否有回弹
//    [self createData];
    // 主页面 view
    [self.view addSubview:_mainScroll];
    
//    [self sdjkfsd];
    
    [self.mainScroll release];
}

-(void)createData{
    
    RequesetManager *requeset = [[RequesetManager alloc] initWithRequestURL:@"http://api2.pianke.me/ting/info" requestModel:POST parameyer:nil];
    requeset.parameyerDict = [@{@"client":@"1",@"deviceid":@"6672A5D6-269D-4BAC-9227-3ACBBDFB9FD3",@"tingid":self.tingID,@"auth":@"XcRoQyXm19KlDCMd2GsfplzJxQ8SrNeOPpzw9UGRicWP23tFEQhVd6PM",@"version":@"3.0.2"} mutableCopy];
    requeset.delegate = self;
    [requeset startRequest];
    
    [requeset release];

    
  
    
}
//JSON
-(void)requestDidFinshWithData:(NSData *)data
{
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
    NSDictionary *dict1 = dict[@"data"];
    self.model = [[RadioModel alloc] init]; //初始化
    [self.model setValuesForKeysWithDictionary:dict1]; //把data给当个model
    
    NSArray *arr = dict1[@"moreting"]; //第四页面的其他人内容
    for (NSDictionary *dict in arr) {
        RadioModel *model = [[RadioModel alloc] init];
        [model setValuesForKeysWithDictionary:dict];
        [self.fourArr addObject:model];
        [model release];
    }
    
    
    [self createMianView]; //创建第二站试图内容
    [self createColletionView ]; //第四个界面 内容
    
    
    
}
-(void)requestError:(NSError *)error{
    NSLog(@"error:%@",error);
}


-(void)createMianView{
//    主页面大图
    
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH + 50, 50, SCREEN_WIDTH - 100, SCREEN_HiGHT/2 - 70)];
    [_imageView sd_setImageWithURL:[NSURL URLWithString:_model.playInfo[@"imgUrl"]]];
    [self.mainScroll addSubview:_imageView];
//    名字
    [self.nameLabel removeFromSuperview];
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH , SCREEN_HiGHT /2+10 , SCREEN_WIDTH , 40)];
    _nameLabel.text = _model.playInfo[@"title"];
    _nameLabel.font = [UIFont systemFontOfSize:25];
    _nameLabel.textAlignment = NSTextAlignmentCenter; //居中
    [self.mainScroll addSubview:_nameLabel];

//    喜欢&评论
    self.Label = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH , SCREEN_HiGHT/2+75, SCREEN_WIDTH, 30)];
    UILabel *liek = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH/4, 5, 50, 20)];
    liek.text = [NSString stringWithFormat:@"%@",_model.like]; //因为他不字符串 想要用这个转换
    UILabel *comment = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH - 80, 5, 50, 20)];
    comment.text = [NSString stringWithFormat:@"%@",_model.comment]; //评论
    comment.font = [UIFont systemFontOfSize:13]; //字体大小
    liek.font = [UIFont systemFontOfSize:13];
    [comment setTextColor:[UIColor grayColor]];//字体颜色
    [liek setTextColor:[UIColor grayColor]];
    UIImageView *imageLiek = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"xing"]];
    imageLiek.frame = CGRectMake(SCREEN_WIDTH/4 - 25, 5, 20, 20); //位置
    UIImageView *imageComment = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"comment"]];
    [_Label addSubview:imageComment];
    imageComment.frame = CGRectMake(SCREEN_WIDTH - 80 - 25, 5, 20, 20);
    [_Label addSubview:imageLiek];
    [_Label addSubview:comment];
    
    [_Label addSubview:liek];
    [self.mainScroll addSubview:_Label];
//    下载进度条
    UILabel *jindutiao = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH, SCREEN_HiGHT/2+130, SCREEN_WIDTH, 30)];
    UIButton *player = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [player addTarget:self action:@selector(playerButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    
    [self.mainScroll addSubview:jindutiao];
    
    [_imageView release];
    
    [_nameLabel release];
    [liek release];
    [_Label release];
    [comment release];
//    [jindutiao release];
}

-(void)createWebView{
    UIWebView *web = [[UIWebView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH *2, 0, SCREEN_WIDTH, SCREEN_HiGHT-70)];
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://pianke.me/webview/556fb54ddfa688425a000033"]]];
    [self.mainScroll addSubview:web];
}

-(void)sdjkfsd{
    
    _player = [PlayerManager sharedManager];//播放器初始化
    _player.playerArr = @[ @"http://fdfs.xmcdn.com/group4/M03/7E/69/wKgDs1PJ-fKx8yQtAEuOLNStMgk721.mp3",
                           @"http://fdfs.xmcdn.com/group4/M09/28/A9/wKgDs1QgAKnhwOCxAIn2i-ieFOo763.mp3",
                           @"http://fdfs.xmcdn.com/group6/M03/B9/56/wKgDhFUP6LjzFEwjAHZ_rwkSG7g294.mp3",
                           @"http://fdfs.xmcdn.com/group4/M06/93/4A/wKgDs1RRrtKTxnuLAFJrufqKR6k504.mp3",
                           @"http://fdfs.xmcdn.com/group6/M06/21/EB/wKgDg1TZkgrgnmmpAGwHr5T3Wnw284.mp3",
                           @"http://fdfs.xmcdn.com/group5/M04/A4/44/wKgDtVRz8v2SoyzoAFDCa7TS6uc267.mp3",
                           @"http://fdfs.xmcdn.com/group9/M01/09/86/wKgDZlVfA-6gcP3oAQYWDXqdG-4370.mp3"]; //添加歌曲链接
    [_player play];
    NSTimer *time = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(musicIsPlayer) userInfo:nil repeats:YES];
    //        添加runLoop
    [[NSRunLoop currentRunLoop] addTimer:time forMode:NSDefaultRunLoopMode];
    
}
//音频方法
-(void)musicIsPlayer
{
    
}
-(void)playerButtonAction:(UIButton *)button{
    
    [_player play];
}

//创建第一个界面  tabelView
-(void)createTabelView{
    self.tabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HiGHT - 120)];
    _tabelView.delegate = self;
    _tabelView.dataSource = self;
    [self.tabelView registerClass:[PlayerTableViewCell class] forCellReuseIdentifier:@"tabel"];
    [self.mainScroll addSubview:_tabelView];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.upArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tabel" forIndexPath:indexPath];
    RadioModel *model = self.upArr[indexPath.row];
    cell.model = model;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

//创建第四个界面
-(void)createColletionView{
    
    self.layout = [[UICollectionViewFlowLayout alloc] init];
    _layout.minimumInteritemSpacing = 10;
    _layout.minimumLineSpacing = 10;
    _layout.itemSize = CGSizeMake(100,100);
    _layout.headerReferenceSize = CGSizeMake(0, 10);
    _layout.footerReferenceSize = CGSizeMake(0, 20);
//    _layout
    self.colletion =[[UICollectionView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH *3+20, SCREEN_HiGHT / 2 -70, SCREEN_WIDTH - 40, SCREEN_HiGHT / 2 - 70) collectionViewLayout:_layout];
    _colletion.delegate = self;
    _colletion.dataSource = self; // 代理
    _colletion.contentSize = CGSizeMake(0, 0);
    _colletion.bounces = NO;
    _colletion.backgroundColor = [UIColor whiteColor]; // 背景变白
    [_colletion registerClass:[playerCollectionViewCell class] forCellWithReuseIdentifier:@"collectionViewCell"]; //注册
    [self.mainScroll addSubview:self.colletion];
    
//    分割线
    self.lineView = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH *3, SCREEN_HiGHT/2-90, SCREEN_WIDTH, 20)];
    UILabel *namelabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, 100, 20)];
    namelabel.text = @"主播其他作品";
    namelabel.font = [UIFont systemFontOfSize:13];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 10, SCREEN_WIDTH - 100 , 0.5)];
    label.backgroundColor = [UIColor grayColor];
    label.alpha = .3;//透明度
    
    [self.lineView addSubview:label];
    [self.lineView addSubview:namelabel];
    [self.mainScroll addSubview:self.lineView];
    [namelabel release];
    [label release];
    
    self.userNameImage2 = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 3 +80, 80, 30, 30)]; //作者头像
    [self imageWithRound:self.userNameImage2 sum:15]; //变圆方法;
    NSDictionary *dict = _model.authorinfo; //其他人
    [self.userNameImage2 sd_setImageWithURL:[NSURL URLWithString:dict[@"icon"]]]; //头像
    self.userNameLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 3 + 130, 85, 100, 20)];
    self.userNameLabel2.font = [UIFont systemFontOfSize:12]; //大小
    self.userNameLabel2.text = dict[@"uname"]; //名字
    self.upNameImage1 = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 3 +80, 30, 30, 30)];
    [self imageWithRound:self.upNameImage1 sum:15]; //变圆
    NSDictionary *updict = _model.userinfo;
    [self.upNameImage1 sd_setImageWithURL:[NSURL URLWithString:updict[@"icon"]]]; //头像
    self.userNameLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH * 3 +130, 35, 100, 20)];
    self.userNameLabel1.text = updict[@"uname"]; //名字
    self.userNameLabel1.font = [UIFont systemFontOfSize:12];
    
    [self.mainScroll addSubview:self.upNameImage1];
    [self.mainScroll addSubview:self.userNameLabel1];
    [self.mainScroll addSubview:self.userNameLabel2];
    [self.mainScroll addSubview:self.userNameImage2];
    
    
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*3+10, 30, 70, 30)];
    label1.text  = @"主播:";
    [self.mainScroll addSubview:label1];
    [label1 release];
    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*3+10, 80, 70, 30)];
    label2.text  = @"原文:";
    [self.mainScroll addSubview:label2];
    [label2 release];
    UILabel *label3=[[UILabel alloc] initWithFrame:CGRectMake(SCREEN_WIDTH*3+10, 160, SCREEN_WIDTH - 10, 30)];
    label3.text = [NSString stringWithFormat:@"来着电台: %@",_model.radioname];
    [self.mainScroll addSubview:label3];
    [label3 release];
    
    
}
//图片设置 的方法
-(void)imageWithRound:(UIImageView *)image sum:(int)sum{
    image.layer.cornerRadius = sum;// 角少多少幅度
    image.layer.masksToBounds = YES; // 是否消失
    [self.mainScroll addSubview:image];
}
#pragma mark - UIcollaytionDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.fourArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    playerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"collectionViewCell" forIndexPath:indexPath];
    RadioModel *model = self.fourArr[indexPath.row];
    [cell setModel:model] ;
    [model release];
    return cell;
}


//播放导航条
-(void)createPlayerNavi
{
//    上按钮
    UIButton *upButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    upButton.frame = CGRectMake(50, SCREEN_HiGHT - 45, 30, 30);
    [upButton addTarget:self action:@selector(upButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [upButton setImage:[UIImage imageNamed:@"up"] forState:(UIControlStateNormal)];
    [self.view addSubview:upButton];
//    下按钮
    UIButton *underButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    underButton.frame = CGRectMake(SCREEN_WIDTH - 70, SCREEN_HiGHT - 45, 30, 30);
    [underButton addTarget:self action:@selector(underButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [underButton setImage:[UIImage imageNamed:@"down"] forState:(UIControlStateNormal)];
    [self.view addSubview:underButton];
//    中间按钮 暂停
    self.switchButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _switchButton.frame = CGRectMake(SCREEN_WIDTH/2 - 20, SCREEN_HiGHT - 50, 40, 40);
    [_switchButton addTarget:self action:@selector(switchButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [_switchButton setImage:[UIImage imageNamed:@"player"] forState:(UIControlStateNormal)];
    [self.view addSubview:_switchButton];
}
//向上一首的按钮
-(void)upButtonAction:(UIButton *)button
{
    NSLog(@"上一首");
}
//下一首的按钮
-(void)underButtonAction:(UIButton *)button
{
    NSLog(@"下一首");
    
    RadioModel *model =  _upArr[_i++];
    _tingID = model.tingid;
    [self createData];
}
-(void)switchButtonAction:(UIButton *)button
{
    if (_isPlayer) {
        [_switchButton setImage:[UIImage imageNamed:@"stop"] forState:(UIControlStateNormal)];
        _isPlayer = NO;
    }
    else
    {
        [_switchButton setImage:[UIImage imageNamed:@"player"] forState:(UIControlStateNormal)];
        _isPlayer = YES;
    }
    
}
//上面导航条的辅助按钮
-(void)createTitelNavi{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)handleChangeFramButtonAction:(UIButton *)button
{
    [self.navigationController popViewControllerAnimated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
